

import datetime

from openpyxl.styles.builtins import total

from common.public import *
from orm.model import RegistrationSheet, db
from view.baseview import BaseView


class RegistrationSheetView(BaseView):
    def __init__(self):
        pass
    @db.atomic()
    def addRegistration(self, params):
        """
        添加我的客户
        """
        registrationsheet = RegistrationSheet.create(
            name = params["name"],
            sex = params["sex"],
            birthday = params["birthday"],
            position = params["position"],
            local = params["local"],
            marital_status = params["maritalStatus"],
            identity_card = params["identityCard"],
            tel = params["tel"],
            registered_permanent_residence = params["registeredPermanentResidence"],
            residential_address = params["residentialAddress"],
            urgent_name = params["urgentName"],
            urgent_tel = params["urgentTel"],
            salary_expectation = params["salaryExpectation"],
            fastest_work_time = params["fastestWorkTime"],
            learn_start_time = params["learnStartTime"],
            learn_end_time = params["learnEndTime"],
            school_name = params["schoolName"],
            examination_type = params["examinationType"],
            major = params["major"],
            graduate_type = params["graduateType"],
            educationlist = params["educationList"],
            add_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()),
            update_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()),
        )
        return super().resultSet(200, "添加成功", [])
        raise Exception("添加失败")

    def checkRegistration(self, params):
        """根据条件取得我的客户列表，params：条件参数"""
        result = []
        where_condition = True
        if params["tel"]:
            where_condition = where_condition & (RegistrationSheet.tel == params["tel"])
        if params["identityCard"]:
            where_condition = where_condition & (RegistrationSheet.identity_card == params["identityCard"])
        total = RegistrationSheet.select().where(where_condition).count()
        rows = RegistrationSheet.select().where(where_condition)
        if total != 0:
            for row in rows: 
                data = {
                        "name":row.name,
                        "sex":row.sex,
                        "birthday":row.birthday,
                        "position":row.position,
                        "local":row.local,
                        "maritalStatus":row.marital_status,
                        "identityCard":row.identity_card,
                        "tel":row.tel,
                        "registeredPermanentResidence":row.registered_permanent_residence,
                        "residentialAddress":row.residential_address,
                        "urgentName":row.urgent_name,
                        "urgentTel":row.urgent_tel,
                        "salaryExpectation":row.salary_expectation,
                        "fastestWorkTime":row.fastest_work_time,
                        "learnStartTime":row.learn_start_time,
                        "learnEndTime":row.learn_end_time,
                        "schoolName":row.school_name,
                        "examinationType":row.examination_type,
                        "major":row.major,
                        "graduateType":row.graduate_type,
                        "educationList":row.educationlist,
                        "nation": row.nation,
                        "stature":row.stature,
                        "weight":row.weight,
                        "eMail":row.e_mail,
                        "health":row.health,
                        "certificateLevel":row.certificate_level,
                        "englishLevel":row.english_level,
                        "selfComment":row.self_comment,
                        "specialty":row.specialty,
                        "workexperience":row.workexperience,
                        "family":row.family,
                        "dataUrl":row.imgbase64             
                       } 
            result.append(data)
        return super().resultSet(200, "ok" , result, total) 


if __name__ == '__main__': 
    view = RegistrationSheetView()
    data = view.addRegistration()

